package cate.game.role.res.cross_sync;

import cate.common.util.TimeTool;
import cate.game.role.RoleDependent;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.Random;

@NoteClass("跨服信息同布")
public class RoleCrossSync extends RoleDependent {
    @NoteField("下次同布主线时间")
    private long nextSyncAdventureTime;
    @NoteField("下次同布快照时间")
    private long nextSyncSnapshotTime;

    @Override
    public void onEnter() {
        super.onEnter();
        if (syncAdventureCheck()) {
            syncAdventure();
        }
        if (syncSnapshotCheck()) {
            syncSnapshot();
        }
        if (syncSnapshotCheckGroup()) {
            syncSnapshotGroup();
        }
        // 随机10-12分钟后再次同布
        nextSyncSnapshotTime = System.currentTimeMillis()
                + new Random().nextInt((int) (2 * TimeTool.TimeCons.MINUTE))
                + 5 * TimeTool.TimeCons.MINUTE;
    }

    @Override
    protected void offline() {
        super.offline();
        if (syncAdventureCheck()) {
            syncAdventure();
        }
        if (syncSnapshotCheck()) {
            syncSnapshot();
        }
        if (syncSnapshotCheckGroup()) {
            syncSnapshotGroup();
        }
    }

    /**
     * 循环
     */
    public void tick() {
        long nowTime = System.currentTimeMillis();
        if (syncAdventureCheck() && nowTime >= nextSyncAdventureTime) {
            syncAdventure();
        }
        if (nowTime >= nextSyncSnapshotTime) {
            if (syncSnapshotCheck()) {
                syncSnapshot();
            }
            if (syncSnapshotCheckGroup()) {
                syncSnapshotGroup();
            }
            // 随机10-12分钟后再次同布
            nextSyncSnapshotTime = System.currentTimeMillis()
                    + new Random().nextInt((int) (2 * TimeTool.TimeCons.MINUTE))
                    + 5 * TimeTool.TimeCons.MINUTE;
        }
    }

    /**
     * 是否同布主线阵容
     */
    private boolean syncAdventureCheck() {
        return role.getPvp().jungle.isOpen();
    }

    /**
     * 是否同布快照
     */
    private boolean syncSnapshotCheck() {
        return role.getPvp().ladder.isOpen()
                || role.getPvp().championRace.isOpen()
                || role.getPvp().arena.isOpen();
    }

    /**
     * 是否同布快照到Group
     */
    private boolean syncSnapshotCheckGroup() {
        return role.getPvp().teamPvp.isOpen();
    }

    /**
     * 同布主线阵容
     */
    public void syncAdventure() {
        game().cross.syncAdventure(role);
        // 随机10-12分钟后再次同布
        nextSyncAdventureTime = System.currentTimeMillis()
                + new Random().nextInt((int) (2 * TimeTool.TimeCons.MINUTE))
                + 10 * TimeTool.TimeCons.MINUTE;
    }

    /**
     * 同布快照
     */
    public void syncSnapshot() {
        game().cross.syncSnapshot(role);
    }

    /**
     * 同布快照Group
     */
    public void syncSnapshotGroup() {
        game().cross.syncSnapshotGroup(role);
    }
}
